package com.example.performance.model;

/**
 * HTTP请求执行结果模型
 * 记录单次请求的执行结果和统计信息
 */
public class RequestResult {

    private String requestName; // 请求名称
    private String url; // 请求URL
    private String method; // 请求方法
    private long startTime; // 开始时间（毫秒时间戳）
    private long endTime; // 结束时间（毫秒时间戳）
    private long responseTime; // 响应时间（毫秒）
    private int statusCode; // HTTP状态码
    private String responseBody; // 响应内容
    private boolean success; // 是否成功
    private String errorMessage; // 错误消息
    private long responseSize; // 响应大小（字节）
    private String threadName; // 执行线程名称

    // 构造函数
    public RequestResult() {
    }

    public RequestResult(String requestName, String url, String method) {
        this.requestName = requestName;
        this.url = url;
        this.method = method;
        this.startTime = System.currentTimeMillis();
    }

    // Getter和Setter方法
    public String getRequestName() {
        return requestName;
    }

    public void setRequestName(String requestName) {
        this.requestName = requestName;
    }

    public String getUrl() {
        return url;
    }

    public void setUrl(String url) {
        this.url = url;
    }

    public String getMethod() {
        return method;
    }

    public void setMethod(String method) {
        this.method = method;
    }

    public long getStartTime() {
        return startTime;
    }

    public void setStartTime(long startTime) {
        this.startTime = startTime;
    }

    public long getEndTime() {
        return endTime;
    }

    public void setEndTime(long endTime) {
        this.endTime = endTime;
        this.responseTime = endTime - startTime;
    }

    public long getResponseTime() {
        return responseTime;
    }

    public void setResponseTime(long responseTime) {
        this.responseTime = responseTime;
    }

    public int getStatusCode() {
        return statusCode;
    }

    public void setStatusCode(int statusCode) {
        this.statusCode = statusCode;
    }

    public String getResponseBody() {
        return responseBody;
    }

    public void setResponseBody(String responseBody) {
        this.responseBody = responseBody;
        if (responseBody != null) {
            this.responseSize = responseBody.getBytes().length;
        }
    }

    public boolean isSuccess() {
        return success;
    }

    public void setSuccess(boolean success) {
        this.success = success;
    }

    public String getErrorMessage() {
        return errorMessage;
    }

    public void setErrorMessage(String errorMessage) {
        this.errorMessage = errorMessage;
    }

    public long getResponseSize() {
        return responseSize;
    }

    public void setResponseSize(long responseSize) {
        this.responseSize = responseSize;
    }

    public String getThreadName() {
        return threadName;
    }

    public void setThreadName(String threadName) {
        this.threadName = threadName;
    }

    /**
     * 标记请求完成
     */
    public void markComplete() {
        this.endTime = System.currentTimeMillis();
        this.responseTime = this.endTime - this.startTime;
        this.threadName = Thread.currentThread().getName();
    }

    /**
     * 标记请求成功
     * 
     * @param statusCode   HTTP状态码
     * @param responseBody 响应内容
     */
    public void markSuccess(int statusCode, String responseBody) {
        markComplete();
        this.success = true;
        this.statusCode = statusCode;
        setResponseBody(responseBody);
    }

    /**
     * 标记请求失败
     * 
     * @param errorMessage 错误消息
     */
    public void markFailure(String errorMessage) {
        markComplete();
        this.success = false;
        this.errorMessage = errorMessage;
    }

    /**
     * 标记请求失败，带状态码
     * 
     * @param statusCode   HTTP状态码
     * @param errorMessage 错误消息
     */
    public void markFailure(int statusCode, String errorMessage) {
        markFailure(errorMessage);
        this.statusCode = statusCode;
    }

    @Override
    public String toString() {
        return "RequestResult{" +
                "requestName='" + requestName + '\'' +
                ", method='" + method + '\'' +
                ", url='" + url + '\'' +
                ", responseTime=" + responseTime + "ms" +
                ", statusCode=" + statusCode +
                ", success=" + success +
                '}';
    }
}